<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Navs;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class NavsController extends CommonController
{
    //  get admin/navs
	public function index()
	{
		$data = Navs::orderBy('nav_order', 'asc')->get();
		return view('admin.navs.index', compact('data'));
	}

	//get admin/navs/create
	public function create()
	{
		return view('admin.navs.create');
	}

	//post admin/navs  负责添加自定义导航的存储
	public function store()
	{
		$input = Input::except('_token');
		$rules = [
			'nav_name' => 'required',
			'nav_url' => 'required',
		];
		$message = [
			'nav_name.required' => '导航名称名称不能为空',
			'nav_url.required' => '导航URL不能为空',

		];
		$validator = Validator::make($input, $rules, $message);

		if ($validator->passes()) {
			$result = Navs::create($input);
			if ($result) {
				return redirect('admin/navs');
			} else {
				return back()->with('errors', '导航添加失败，请稍后重试');
			}

		} else {
			return back()->withErrors($validator);
		}
	}

	//get admin/navs/{navs}/edit
	public function edit($nav_id)
	{
		$field = Navs::find($nav_id);
		return view('admin.navs.edit', compact('field'));
	}

	//put admin/navs/{navs}
	public function update($nav_id)
	{
		$input = Input::except('_token', '_method');
		$result = Navs::where('nav_id', $nav_id)->update($input);
		if ($result) {
			return redirect('admin/navs');
		} else {
			return back()->with('errors', '导航更新失败，请稍后重试');
		}
	}

	//delete admin/navs/{navs}
	public function destroy($nav_id)
	{
		$result = Navs::where('nav_id', $nav_id)->delete();
		if ($result) {
			$data = [
				'status' => 0,
				'msg' => '导航删除成功'
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '导航删除失败'
			];
		}
		return $data;
	}

	public function changeOrder()
	{
		$input = Input::all();
		$link = Navs::find($input['nav_id']);
		$link->nav_order = $input['nav_order'];
		$result = $link->update();
		if($result) {
			$data = [
				'status' => 0,
				'msg' => '自定义导航排序更新成功',
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '自定义导航排序更新失败，请稍后重试',
			];
		}
		return $data;
	}

	public function show()
	{

	}
}
